<?php
namespace Operationtest\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManagerInterface;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;

class SyntheseController extends AbstractActionController
{
    protected $saleheadTable;
    protected $salerowTable;
    protected $storeTable;
    protected $meteoTable;
    protected $storecountTable;
    protected $marcheTable;
    protected $marcheStoreTable;
    protected $rhdataTable;
    
    public function setEventManager(EventManagerInterface $events) {
        parent::setEventManager($events);
 
        $controller = $this;
        $events->attach('dispatch', function ($e) use ($controller) {
            $controller->layout()->setTemplate('layout/operationtest');
        }, 100);
 
        return $this;
    }
    
    public function getSaleheadTable() {
        if (!$this->saleheadTable) {
            $sm = $this->getServiceLocator();
            $this->saleheadTable = $sm->get('Operationtest\Model\SaleheadTable');
        }
        return $this->saleheadTable;
    }
    
    public function getSalerowTable() {
        if (!$this->salerowTable) {
            $sm = $this->getServiceLocator();
            $this->salerowTable = $sm->get('Operationtest\Model\SalerowTable');
        }
        return $this->salerowTable;
    }
    
    public function getStoreTable() {
        if (!$this->storeTable) {
            $sm = $this->getServiceLocator();
            $this->storeTable = $sm->get('Operationtest\Model\StoreTable');
        }
        return $this->storeTable;
    }
    
    public function getMeteoTable() {
        if (!$this->meteoTable) {
            $sm = $this->getServiceLocator();
            $this->meteoTable = $sm->get('Operationtest\Model\MeteoTable');
        }
        return $this->meteoTable;
    }
    
    public function getStorecountTable() {
        if (!$this->storecountTable) {
            $sm = $this->getServiceLocator();
            $this->storecountTable = $sm->get('Operationtest\Model\StorecountTable');
        }
        return $this->storecountTable;
    }
    
    public function getMarchestoreTable() {
        if (!$this->marcheStoreTable) {
            $sm = $this->getServiceLocator();
            $this->marcheStoreTable = $sm->get('Operationtest\Model\MarchestoreTable');
        }
        return $this->marcheStoreTable;
    }
    
    public function getMarcheTable() {
        if (!$this->marcheTable) {
            $sm = $this->getServiceLocator();
            $this->marcheTable = $sm->get('Operationtest\Model\MarcheTable');
        }
        return $this->marcheTable;
    }
    
    public function getRhdataTable() {
        if (!$this->rhdataTable) {
            $sm = $this->getServiceLocator();
            $this->rhdataTable = $sm->get('Operationtest\Model\RhdataTable');
        }
        return $this->rhdataTable;
    }
    
    public function indexAction()
    {
        $request = $this->getRequest();
        if($request->isPost()) {
            $query = $this->getQuery($request);
            $groupby = " GROUP BY saleHead.orderDate";            
            $ecgroupby = " GROUP BY date";            
            $rptgroupby = " GROUP BY saleHead.orderDate, Article.idArticle";
            $tmgroupby = " GROUP BY saleHead.orderDate, type"; 
            
            $groupby2 = " GROUP BY saleHead.idStore, saleHead.orderDate";
            $ecgroupby2 = " GROUP BY idStore, date";
            
            // MISE EN SESSION DES CRITERES DANS SELECTION DU PERIMETRE
            $dataCritere = new Container('dataCritere');
            $dataCritere->getManager()->getStorage()->clear('dataCritere');
            
            if($request->getPost()->periodY)
                $dataCritere->periodY = $request->getPost()->periodY;
            if($request->getPost()->periodM)
                $dataCritere->periodM = $request->getPost()->periodM;
            if($request->getPost()->semaine)
                $dataCritere->semaine = $request->getPost()->semaine;
            if($request->getPost()->specific_date)
                $dataCritere->specific_date = $request->getPost()->specific_date;
            
            // MISE EN SESSION DES VALEURS POUR LES RECUPERER DANS UNE AUTRE ACTION
            $dataSynthese = new Container('dataSynthese');
            $dataSynthese->ca = $this->getSaleheadTable()->getSynthCa($query['where'], $groupby);            
            $dataSynthese->tm = $this->getSaleheadTable()->getSynthTmTtc($query['where'], $tmgroupby);
            $dataSynthese->nbT = $this->getSaleheadTable()->getSynthTransaction($query['where'], $groupby);            
            $dataSynthese->nbA = $this->getSalerowTable()->getNbArticle($query['where'], $groupby);
            $dataSynthese->rptProduit = $this->getSalerowTable()->getRapportProduit($query['where'], $rptgroupby);            
            $dataSynthese->period = $this->displayPeriod($request);
            $dataSynthese->meteoDays = $this->getMeteoTable()->getSMeteos($query['whereOther']);
            $dataSynthese->ecDays = $this->getStorecountTable()->getSynthEcarts($query['whereOther'], $ecgroupby);            
            $dataSynthese->repasDays = $this->getSaleheadTable()->getSynthRepas($query['where'], $groupby);
            $dataSynthese->ac = $this->getSaleheadTable()->getSynthAnnulationCaisse($query['where'], $groupby);
            $dataSynthese->banque = $this->getStorecountTable()->getSynthBanques($query['whereOther'], $ecgroupby);  
            $dataSynthese->mo = $this->getRhdataTable()->getSynthMO($query['whereOther'], $ecgroupby);  
            
            $dataSynthese->ca2 = $this->getSaleheadTable()->getSynthCa($query['where'], $groupby2);
            $dataSynthese->nbT2 = $this->getSaleheadTable()->getSynthTransaction($query['where'], $groupby2);
            $dataSynthese->nbA2 = $this->getSalerowTable()->getNbArticle($query['where'], $groupby2);
            $dataSynthese->ecDays2 = $this->getStorecountTable()->getSynthEcarts($query['whereOther'], $ecgroupby2);
            $dataSynthese->repasDays2 = $this->getSaleheadTable()->getSynthRepas($query['where'], $groupby2);
            $dataSynthese->ac2 = $this->getSaleheadTable()->getSynthAnnulationCaisse($query['where'], $groupby2);
            $dataSynthese->banque2 = $this->getStorecountTable()->getSynthBanques($query['whereOther'], $ecgroupby2);
            $dataSynthese->mo2 = $this->getRhdataTable()->getSynthMO($query['whereOther'], $ecgroupby2);
            
            $tabStore = array();
            foreach($request->getPost()->selectable as $storeId){
                $tabStoreId = explode("-", $storeId);
                if(count($tabStoreId) == 1) { // Récupères les boutiques choisies
                    $storeItem = $this->getStoreTable()->getStore($storeId);
                    $tabStore[$storeItem->idStore] = $storeItem->name;
                } else { // Récupère les boutiques du marché choisi
                    $mStores = $this->getMarchestoreTable()->getMarcheStore($tabStoreId[1]);
                    if($tabStoreId[1] == 1) { // Si le marché Cojean est choisi
                        $stores = $this->getStoreTable()->fetchAll();
                        foreach ($stores as $store) {
                            $storeItem = $this->getStoreTable()->getStore($store->idStore);
                            $tabStore[$storeItem->idStore] = $storeItem->name;
                        }
                    } else { // Si autre marché que Cojean est choisi
                        foreach($mStores as $mstore) {
                            $storeItem = $this->getStoreTable()->getStore($mstore['idStore']);
                            $tabStore[$storeItem->idStore] = $storeItem->name;
                        }
                    }
                }
            }
            
            $dataSynthese->store = $tabStore;
            
            $storeName = "";
            foreach($request->getPost()->selectable as $storeId){
                $tabStoreId = explode("-", $storeId);
                if(count($tabStoreId) == 1) {
                    $store = $this->getStoreTable()->getStore($storeId);
                    if($storeName == "")
                        $storeName .= $store->name;
                    else
                        $storeName .= " - ".$store->name;
                } else {
                    $marche = $this->getMarcheTable()->getMarche($tabStoreId[1]);
                    if($storeName == "")
                        $storeName .= $marche->libelle;
                    else
                        $storeName .= " - ".$marche->libelle;
                }
            }
            
            return new ViewModel(array(
                'monthText' => $this->echoMonthText(),

                ///////// PERIODE TEXTE
                'periodText' => $query['period'],

                ///////// STORE
                'store' => $storeName,

                ///////// AFFICHAGE DES SEMAINES
                'period' => $this->displayPeriod($request),
            ));
        } else {
            return $this->redirect()->toRoute('operationtest/default');
        }
    }
    
    public function syntheseAction() {
        $dataSynthese = new Container('dataSynthese');
        $variables = array(
            'ca' => $dataSynthese->offsetGet('ca'),
            'tm' => $dataSynthese->offsetGet('tm'),
            'nbT' => $dataSynthese->offsetGet('nbT'),
            'nbA' => $dataSynthese->offsetGet('nbA'),
            'period' => $dataSynthese->offsetGet('period'),
            'echoMonthtoEn' => $this->echoMonthtoEn(),
            'meteoDays' => $dataSynthese->offsetGet('meteoDays'),
            'ecDays' => $dataSynthese->offsetGet('ecDays'), 
            'repasDays' => $dataSynthese->offsetGet('repasDays'),
            'ac' => $dataSynthese->offsetGet('ac'),
            'banque' => $dataSynthese->offsetGet('banque'),
            'mo' => $dataSynthese->offsetGet('mo'),
        );
        
        $viewModel = new ViewModel();
        $viewModel->setVariables($variables);
        $viewModel->setTerminal(true);
        
        return $viewModel;
    }
    
    public function makegraphiqueAction() {
        $dataSynthese = new Container('dataSynthese');
        $variables = array(
            'id' => $this->params('id'),
            'ca' => $dataSynthese->offsetGet('ca'),
            'tm' => $dataSynthese->offsetGet('tm'),
            'nbT' => $dataSynthese->offsetGet('nbT'),
            'nbA' => $dataSynthese->offsetGet('nbA'),
            'meteoDays' => $dataSynthese->offsetGet('meteoDays'),
            'ecDays' => $dataSynthese->offsetGet('ecDays'), 
            'repasDays' => $dataSynthese->offsetGet('repasDays'),
            'ac' => $dataSynthese->offsetGet('ac'),
            'banque' => $dataSynthese->offsetGet('banque'),
            'mo' => $dataSynthese->offsetGet('mo'),
        );
        
        $viewModel = new ViewModel();
        $viewModel->setVariables($variables);
        $viewModel->setTerminal(true);
        
        return $viewModel;
    }
    
    public function graphiqueAction() {        
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        
        return $viewModel;
    }
    
    public function detailsAction() {
        $dataSynthese = new Container('dataSynthese');
        $variables = array(
            'ca' => $dataSynthese->offsetGet('ca2'),
            'tm' => $dataSynthese->offsetGet('tm'),
            'nbT' => $dataSynthese->offsetGet('nbT2'),
            'nbA' => $dataSynthese->offsetGet('nbA2'),
            'period' => $dataSynthese->offsetGet('period'),
            'store' => $dataSynthese->offsetGet('store'),
            'monthText' => $this->echoMonthText(),
            'echoMonthtoEn' => $this->echoMonthtoEn(),
            'ec' => $dataSynthese->offsetGet('ecDays2'), 
            'repas' => $dataSynthese->offsetGet('repasDays2'),
            'ac' => $dataSynthese->offsetGet('ac2'),
            'banque' => $dataSynthese->offsetGet('banque2'),
            'mo' => $dataSynthese->offsetGet('mo2'),
        );
        
        $viewModel = new ViewModel();
        $viewModel->setVariables($variables);
        $viewModel->setTerminal(true);
        
        return $viewModel;
    }
    
    public function accessrapportAction() {
        $dataSynthese = new Container('dataSynthese');
        $variables = array(
            'ca' => $dataSynthese->offsetGet('ca'),
            'tm' => $dataSynthese->offsetGet('tm'),
            'nbT' => $dataSynthese->offsetGet('nbT'),
            'nbA' => $dataSynthese->offsetGet('nbA'),
            'period' => $dataSynthese->offsetGet('period'),
            'store' => $dataSynthese->offsetGet('store'),
            'monthText' => $this->echoMonthText(),
        );
        
        $viewModel = new ViewModel();
        $viewModel->setVariables($variables);
        $viewModel->setTerminal(true);
        
        return $viewModel;
    }
    
    public function rapportproduitAction()
    {           
        $dataSynthese = new Container('dataSynthese');
        $variables = array(
            'ca' => $dataSynthese->offsetGet('ca'),
            'tm' => $dataSynthese->offsetGet('tm'),
            'nbT' => $dataSynthese->offsetGet('nbT'),
            'nbA' => $dataSynthese->offsetGet('nbA'),
            'period' => $dataSynthese->offsetGet('period'),
            'rptProduit' => $dataSynthese->offsetGet('rptProduit'),
            'families' => $this->getSalerowTable()->getFamilies(),
            'store' => $dataSynthese->offsetGet('store'),
            'monthText' => $this->echoMonthText(),
        );
        
        $viewModel = new ViewModel();
        $viewModel->setVariables($variables);
        $viewModel->setTerminal(true);
        
        return $viewModel;
    }
    
    function getQuery($request) {
        $storeIds = array();
        foreach($request->getPost()->selectable as $storeId){
            $tabStoreId = explode("-", $storeId);
            if(count($tabStoreId) == 1) { // Récupères les boutiques choisies
                $storeIds[] = $storeId;
            } else { // Récupère les boutiques du marché choisi
                $mStores = $this->getMarchestoreTable()->getMarcheStore($tabStoreId[1]);
                if($tabStoreId[1] == 1) { // Si le marché Cojean est choisi
                    $stores = $this->getStoreTable()->fetchAll();
                    foreach ($stores as $store) {
                        $storeIds[] = $store->idStore;
                    }
                } else { // Si autre marché que Cojean est choisi
                    foreach($mStores as $mstore) {
                        $storeIds[] = $mstore['idStore'];
                    }
                }
            }
        }
        $storeWhere = "idStore IN (".implode(',', $storeIds).")";
        $whereS = $storeWhere;
        $whereOther = $storeWhere;
        $whereS2 = "";
        $whereOther2 = "";
        $query = array();

        if($request->getPost()->periodY){
            $counterA = 0;
            if($request->getPost()->periodM){
                if($request->getPost()->semaine){ ////////////// Année + Mois + Semaine
                    foreach($request->getPost()->periodY as $year):
                        foreach($request->getPost()->periodM as $month):
                            foreach($request->getPost()->semaine as $day):
                                if($whereS2 == "") {
                                    $whereS2 .= " AND ( ( EXTRACT(YEAR FROM orderDate) = ".$year;
                                    $whereS2 .= " AND EXTRACT(MONTH FROM orderDate) = ".$month;
                                    $whereS2 .= " AND DAYOFWEEK(orderDate) = ".($day+1).")"; // (DAYOFWEEK, Dimanche=1 et non 0)
                                } else {
                                    $whereS2 .= " OR ( EXTRACT(YEAR FROM orderDate) = ".$year;
                                    $whereS2 .= " AND EXTRACT(MONTH FROM orderDate) = ".$month;
                                    $whereS2 .= " AND DAYOFWEEK(orderDate) = ".($day+1).")"; // (DAYOFWEEK, Dimanche=1 et non 0)
                                }
                                
                                if($whereOther2 == "") {
                                    $whereOther2 .= " AND ( ( EXTRACT(YEAR FROM date) = ".$year;
                                    $whereOther2 .= " AND EXTRACT(MONTH FROM date) = ".$month;
                                    $whereOther2 .= " AND DAYOFWEEK(date) = ".($day+1).")"; // (DAYOFWEEK, Dimanche=1 et non 0)
                                } else {
                                    $whereOther2 .= " OR ( EXTRACT(YEAR FROM date) = ".$year;
                                    $whereOther2 .= " AND EXTRACT(MONTH FROM date) = ".$month;
                                    $whereOther2 .= " AND DAYOFWEEK(date) = ".($day+1).")"; // (DAYOFWEEK, Dimanche=1 et non 0)
                                }
                            endforeach;
                        endforeach;
                        $counterA++;
                    endforeach;
                } else { ////////////// Année + Mois
                    foreach($request->getPost()->periodY as $year):
                        foreach($request->getPost()->periodM as $month):
                            if($whereS2 == "") {
                                $whereS2 .= " AND ( ( EXTRACT(YEAR FROM orderDate) = ".$year;
                                $whereS2 .= " AND EXTRACT(MONTH FROM orderDate) = ".$month.")";
                            } else {
                                $whereS2 .= " OR ( EXTRACT(YEAR FROM orderDate) = ".$year;
                                $whereS2 .= " AND EXTRACT(MONTH FROM orderDate) = ".$month.")";
                            }
                            
                            if($whereOther2 == "") {
                                $whereOther2 .= " AND ( ( EXTRACT(YEAR FROM date) = ".$year;
                                $whereOther2 .= " AND EXTRACT(MONTH FROM date) = ".$month.")";
                            } else {
                                $whereOther2 .= " OR ( EXTRACT(YEAR FROM date) = ".$year;
                                $whereOther2 .= " AND EXTRACT(MONTH FROM date) = ".$month.")";
                            }
                        endforeach;

                        $counterA++;
                    endforeach;
                }

                if($counterA == count($request->getPost()->periodY)){
                    $whereS2 .= " )";
                    $whereOther2 .= " )";
                }
            } else {
                if($request->getPost()->semaine){ ////////////// Année + Semaine 
                    foreach($request->getPost()->periodY as $year):
                        foreach($request->getPost()->semaine as $day):
                            if($whereS2 == "") {
                                $whereS2 .= " AND ( ( EXTRACT(YEAR FROM orderDate) = ".$year;
                                $whereS2 .= " AND DAYOFWEEK(orderDate) = ".($day+1).")"; // (DAYOFWEEK, Dimanche=1 et non 0)
                            } else {
                                $whereS2 .= " OR ( EXTRACT(YEAR FROM orderDate) = ".$year;
                                $whereS2 .= " AND DAYOFWEEK(orderDate) = ".($day+1).")"; // (DAYOFWEEK, Dimanche=1 et non 0)
                            }
                            
                            if($whereOther2 == "") {
                                $whereOther2 .= " AND ( ( EXTRACT(YEAR FROM date) = ".$year;
                                $whereOther2 .= " AND DAYOFWEEK(date) = ".($day+1).")"; // (DAYOFWEEK, Dimanche=1 et non 0)
                            } else {
                                $whereOther2 .= " OR ( EXTRACT(YEAR FROM date) = ".$year;
                                $whereOther2 .= " AND DAYOFWEEK(date) = ".($day+1).")"; // (DAYOFWEEK, Dimanche=1 et non 0)
                            }
                        endforeach;

                        $counterA++;
                    endforeach;
                } else { ////////////// Année
                    foreach($request->getPost()->periodY as $year):
                        if($whereS2 == "") {
                            $whereS2 .= " AND ( EXTRACT(YEAR FROM orderDate) = ".$year;
                        } else {
                            $whereS2 .= " OR EXTRACT(YEAR FROM orderDate) = ".$year;
                        }
                        
                        if($whereOther2 == "") {
                            $whereOther2 .= " AND ( EXTRACT(YEAR FROM date) = ".$year;
                        } else {
                            $whereOther2 .= " OR EXTRACT(YEAR FROM date) = ".$year;
                        }
                        $counterA++;
                    endforeach;
                }
                if($counterA == count($request->getPost()->periodY)){
                    $whereS2 .= " )";
                    $whereOther2 .= " )";
                }
            } 
        } else {
            if($request->getPost()->periodM){
                $counterM = 0;
                if($request->getPost()->semaine){ ////////////// Mois + Semaine
                    foreach($request->getPost()->periodM as $month):
                        foreach($request->getPost()->semaine as $day):
                            if($whereS2 == "") {
                                $whereS2 .= " AND ( ( EXTRACT(MONTH FROM orderDate) = ".$month;
                                $whereS2 .= " AND DAYOFWEEK(orderDate) = ".($day+1).")"; // (DAYOFWEEK, Dimanche=1 et non 0)
                            } else {
                                $whereS2 .= " OR ( EXTRACT(MONTH FROM orderDate) = ".$month;
                                $whereS2 .= " AND DAYOFWEEK(orderDate) = ".($day+1).")"; // (DAYOFWEEK, Dimanche=1 et non 0)
                            }
                            
                            if($whereOther2 == "") {
                                $whereOther2 .= " AND ( ( EXTRACT(MONTH FROM date) = ".$month;
                                $whereOther2 .= " AND DAYOFWEEK(date) = ".($day+1).")"; // (DAYOFWEEK, Dimanche=1 et non 0)
                            } else {
                                $whereOther2 .= " OR ( EXTRACT(MONTH FROM date) = ".$month;
                                $whereOther2 .= " AND DAYOFWEEK(date) = ".($day+1).")"; // (DAYOFWEEK, Dimanche=1 et non 0)
                            }
                        endforeach;

                        $counterM++;
                    endforeach;
                } else { ////////////// Mois
                    foreach($request->getPost()->periodM as $month):
                        if($whereS2 == "") {
                            $whereS2 .= " AND ( EXTRACT(MONTH FROM orderDate) = ".$month;
                        } else {
                            $whereS2 .= " OR EXTRACT(MONTH FROM orderDate) = ".$month;
                        }
                        
                        if($whereOther2 == "") {
                            $whereOther2 .= " AND ( EXTRACT(MONTH FROM date) = ".$month;
                        } else {
                            $whereOther2 .= " OR EXTRACT(MONTH FROM date) = ".$month;
                        }

                        $counterM++;
                    endforeach;
                }
                if($counterM == count($request->getPost()->periodM)){
                    $whereS2 .= " )";
                    $whereOther2 .= " )";
                }
            } else {
                if($request->getPost()->semaine){  ////////////// Semaine
                    $counterS = 0;
                    foreach($request->getPost()->semaine as $day):
                        if($whereS2 == "") {
                            $whereS2 .= " AND ( DAYOFWEEK(orderDate) = ".($day+1); // (DAYOFWEEK, Dimanche=1 et non 0)
                        } else {
                            $whereS2 .= " OR DAYOFWEEK(orderDate) = ".($day+1); // (DAYOFWEEK, Dimanche=1 et non 0)
                        }
                        
                        if($whereOther2 == "") {
                            $whereOther2 .= " AND ( DAYOFWEEK(date) = ".($day+1); // (DAYOFWEEK, Dimanche=1 et non 0)
                        } else {
                            $whereOther2 .= " OR DAYOFWEEK(date) = ".($day+1); // (DAYOFWEEK, Dimanche=1 et non 0)
                        }

                        $counterS++;
                    endforeach;

                    if($counterS == count($request->getPost()->semaine)) {
                        $whereS2 .= " )";
                        $whereOther2 .= " )";
                    }
                }
            }
        }

        
        $whereS .= $whereS2;        
        $whereOther .= $whereOther2;  
        

        ///////////////////// PERIODE TEXT ///////////////////////
        $period = "";
        $periodM = "";
        $periodY = "";
        $periodJ = "";
        $monthText = $this->echoMonthText();
        $dayText = $this->echoShortDayText();

        if($request->getPost()->periodM){
            $i = 1;
            foreach($request->getPost()->periodM as $mois):
                if($i < count($request->getPost()->periodM))
                    $periodM .= $monthText[$mois].", ";
                else
                    $periodM .= $monthText[$mois];
                $i++;
            endforeach;
        }
        if($request->getPost()->periodY){
            $i = 1;
            $periodY .= ' ';
            foreach($request->getPost()->periodY as $annee):                    
                if($i < count($request->getPost()->periodY))
                    $periodY .= $annee.", ";
                else
                    $periodY .= $annee;
                $i++;
            endforeach;
        }
        if($request->getPost()->semaine){
            $i = 1;
            $periodJ .= ' - ';
            foreach($request->getPost()->semaine as $jour):                    
                if($i < count($request->getPost()->semaine))
                    $periodJ .= $dayText[$jour]." ";
                else
                    $periodJ .= $dayText[$jour];
                $i++;
            endforeach;
        }

        $period = $periodM.$periodY.$periodJ;
        $period = ($period != "")?$period:"Toute la période";

        $query['where'] = $whereS;
        $query['whereOther'] = $whereOther;
        $query['period'] = $period;

        return $query;        
    }
    
    public function echoShortDayText()
    {
        $dayText[0] = 'dim';
        $dayText[1] = 'lun';
        $dayText[2] = 'mar';
        $dayText[3] = 'mer';
        $dayText[4] = 'jeu';
        $dayText[5] = 'ven';
        $dayText[6] = 'sam';        
        
        return $dayText;
    }
    
    public function echoLongDayText()
    {
        $dayText[0] = 'dimanche';
        $dayText[1] = 'lundi';
        $dayText[2] = 'mardi';
        $dayText[3] = 'mercredi';
        $dayText[4] = 'jeudi';
        $dayText[5] = 'vendredi';
        $dayText[6] = 'samedi';        
        
        return $dayText;
    }
    
    public function echoMonthText()
    {
        $monthText[1] = 'Janvier';
        $monthText[2] = 'Fevrier';
        $monthText[3] = 'Mars';
        $monthText[4] = 'Avril';
        $monthText[5] = 'Mai';
        $monthText[6] = 'Juin';
        $monthText[7] = 'Juillet';
        $monthText[8] = 'Août';
        $monthText[9] = 'Septembre';
        $monthText[10] = 'Octobre';
        $monthText[11] = 'Novembre';
        $monthText[12] = 'Décembre';
        
        return $monthText;
    }
    
    public function echoMonthtoEn()
    {
        $monthText['Janvier'] = 'January';
        $monthText['Fevrier'] = 'February';
        $monthText['Mars'] = 'March';
        $monthText['Avril'] = 'April';
        $monthText['Mai'] = 'May';
        $monthText['Juin'] = 'June';
        $monthText['Juillet'] = 'July';
        $monthText['Août'] = 'August';
        $monthText['Septembre'] = 'September';
        $monthText['Octobre'] = 'October';
        $monthText['Novembre'] = 'November';
        $monthText['Décembre'] = 'December';
        
        return $monthText;
    }
    
    public function displayPeriod($request) {
        $monthText = $this->echoMonthText();
        $longDayText = $this->echoLongDayText();
        $weeks = array();
        if($request->getPost()->periodY){
            if($request->getPost()->periodM){
                foreach($request->getPost()->periodY AS $year):
                    foreach($request->getPost()->periodM AS $month):
                        $month = ($month<10)?'0'.$month:$month;
                        $begin = new \DateTime($year.'-'.$month.'-01');
                        $interval = new \DateInterval('P1D');
                        $end = clone $begin;
                        $end->modify('first day of next month');
                        $jours = new \DatePeriod($begin, $interval, $end);
                        foreach($jours AS $jour):                            
                            $mois = $monthText[intval($month)].' '.$year;
                            $semaine = $jour->format('W');
                            $dayText = $longDayText[$jour->format('w')];
                            $weeks[$mois][$semaine][$dayText] = $jour;
                        endforeach;
                    endforeach;
                endforeach;
            } else {
                foreach($request->getPost()->periodY AS $year):
                    for($month=1;$month<13;$month++):
                        $month = ($month<10)?'0'.$month:$month;
                        $begin = new \DateTime($year.'-'.$month.'-01');
                        $interval = new \DateInterval('P1D');
                        $end = clone $begin;
                        $end->modify('first day of next month');
                        $jours = new \DatePeriod($begin, $interval, $end);
                        foreach($jours AS $jour):                            
                            $mois = $monthText[intval($month)].' '.$year;
                            $semaine = $jour->format('W');
                            $dayText = $longDayText[$jour->format('w')];
                            $weeks[$mois][$semaine][$dayText] = $jour;
                        endforeach;
                    endfor;
                endforeach;
            }
        } else {
            if($request->getPost()->periodM){
                for($year=date('Y');$year>2011;$year--):
                    foreach($request->getPost()->periodM AS $month):
                        $month = ($month<10)?'0'.$month:$month;
                        $begin = new \DateTime($year.'-'.$month.'-01');
                        $interval = new \DateInterval('P1D');
                        $end = clone $begin;
                        $end->modify('first day of next month');
                        $jours = new \DatePeriod($begin, $interval, $end);
                        foreach($jours AS $jour):                            
                            $mois = $monthText[intval($month)].' '.$year;
                            $semaine = $jour->format('W');
                            $dayText = $longDayText[$jour->format('w')];
                            $weeks[$mois][$semaine][$dayText] = $jour;
                        endforeach;
                    endforeach;
                endfor;
            } else {
                for($year=date('Y');$year>2011;$year--):
                    for($month=1;$month<13;$month++):
                        $month = ($month<10)?'0'.$month:$month;
                        $begin = new \DateTime($year.'-'.$month.'-01');
                        $interval = new \DateInterval('P1D');
                        $end = clone $begin;
                        $end->modify('first day of next month');
                        $jours = new \DatePeriod($begin, $interval, $end);
                        foreach($jours AS $jour):                            
                            $mois = $monthText[intval($month)].' '.$year;
                            $semaine = $jour->format('W');
                            $dayText = $longDayText[$jour->format('w')];
                            $weeks[$mois][$semaine][$dayText] = $jour;
                        endforeach;
                    endfor;
                endfor;
            }
        }
        return $weeks;
    }
}
?>
